package com.intelli.eve.core.impl.io.remote.protocols.smtp;


import com.intelli.eve.core.impl.configuration.ConfigurationLoader;
import com.intelli.ray.core.ManagedComponent;

import javax.annotation.PostConstruct;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.search.FlagTerm;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author: Sergey42
 * Date: 21.12.2014 18:53
 */
@ManagedComponent(name = "SmtpController")
public class SmtpController {

    protected String host;
    protected boolean debug;
    protected int port;
    protected String user;
    protected String pass;
    protected Map<String, String> extProps;

    @PostConstruct
    protected void init() {
        Map<String, String> properties = new HashMap<>();
        ConfigurationLoader.loadProperties(SmtpController.class, "connection.properties", properties);

        host = properties.get("host");
        debug = Boolean.valueOf(properties.get("debug"));
        port = Integer.valueOf("port");
        user = properties.get("user");
        pass = properties.get("pass");

        extProps = new HashMap<>();
        for (String prop : properties.keySet()) {
            if (prop.startsWith("mail.imap")) extProps.put(prop, properties.get(prop));
        }
    }

    protected Context getUnreadContext() throws MessagingException, IOException {
        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imaps");

        Session emailSession = Session.getInstance(properties);
        if (debug) {
            emailSession.setDebug(true);
        }
        properties.putAll(extProps);

        Store emailStore = emailSession.getStore();
        emailStore.connect(host, port, user, pass);

        Folder emailFolder = emailStore.getFolder("Inbox");
        emailFolder.open(Folder.READ_ONLY);
        List<Message> unreadMessages = Arrays.asList(emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)));

        return new Context(unreadMessages, emailFolder, emailStore);
    }

    protected class Context {
        protected List<Message> unreadMessages;
        protected Folder folder;
        protected Store store;

        public Context(List<Message> unreadMessages, Folder folder, Store store) {
            this.unreadMessages = unreadMessages;
            this.folder = folder;
            this.store = store;
        }
    }

    protected static class MessageInfo {
        protected String subject;
        protected String sentDate;
        protected String emailFrom;
        protected Object content;

        protected static MessageInfo parse(Message message) throws MessagingException, IOException {
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.subject = message.getSubject();
            messageInfo.sentDate = new SimpleDateFormat("dd.MM.yyyy").format(message.getSentDate());
            Address[] from = message.getFrom();
            messageInfo.emailFrom = from == null ? null : ((InternetAddress) from[0]).getAddress();
            messageInfo.content = message.getContent();
            return messageInfo;
        }
    }
}
